
import unittest

from google.appengine.api import mail
from google.appengine.ext import testbed

from email_template import service

class ServiceTest(unittest.TestCase):

  def setUp(self):
    # Setup the email api testbed
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_mail_stub()
    self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)

  def tearDown(self):
    self.testbed.deactivate()

  def test_notify(self):
    data = {
      'topic': 'Eggzactically how to eat eggs.',
      'information': '''Instructions:
      1. Crack open egg shell.
      2. Slurp the contents.
      ''',
    }
    service.Notify('dummy9000', data)

    test_messages = self.mail_stub.get_sent_messages()

    self.assertIsNotNone(test_messages)
    self.assertEqual(1, len(test_messages))
    message = test_messages[0]

    self.assertEqual('double-helix-team@@google.com', message.sender)
    self.assertEqual('dummy@xcorp.com', message.to)
    self.assertEqual('the_coworker.xcorp.com', message.cc)
    self.assertEqual('Eggzactically how to eat eggs.', message.subject)
    self.assertEqual(
      'Hello\n      Eggzactically how to eat eggs.\n\n      To inform you of:\n'
      '      Instructions:\n      1. Crack open egg shell.\n      '
      '2. Slurp the contents.\n      ',
      message.body.payload)


if __name__ == '__main__':
  unittest.main()
